# 1.王者荣耀游戏壁纸下载(https://pvp.qq.com/web201605/wallpaper.shtml)
#    <1>将王者荣耀中游戏壁纸32页的图片下载下来
#    <2>只需要每一张图片的1920*1200分辨率的图片
#    <3>将数据保存到代码指定文件夹中
import os
import re
import urllib  # 内置模块

import requests


def wangzhe(page):
    # 向url发送请求
    url = "https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi"
    # 分离参数
    params = {
        "activityId": "2735",
        "sVerifyCode": "ABCD",
        "sDataType": "JSON",
        "iListNum": "20",
        "totalpage": "0",
        "page": f"{page}",
        "iOrder": "0",
        "iSortNumClose": "1",
        # "jsoncallback": "jQuery1113001504239771366267_1677244014061",
        "iAMSActivityId": "51991",
        "_everyRead": "true",
        "iTypeId": "2",
        "iFlowId": "267733",
        "iActId": "2735",
        "iModuleId": "2735",
        "_": "1677244014063"
    }
    # 模拟浏览器发送请求
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69',
        'referer': 'https://pvp.qq.com/',
        'cookie': 'tvfe_boss_uuid=b895f10c43d59e9b; pgv_pvid=5931020612; RK=IQEpauKd91; ptcz=0c6d86e4c8971018864927a6ede04380c91c57fd5d3c7281854b798b3e4b0576; eas_sid=E126X7Z8q4E542e7w6K082k2q9; ptui_loginuin=2802398205; pgv_info=ssid=s3711677809; pvpqqcomrouteLine=wallpaper_wallpaper'

    }
    # 直接拿到字典数据
    r = requests.get(url, params=params,headers=headers).json()  # '{"name":"allen"}'
    index = 1
    for i in r['List']:
        # 得到编码后的内容
        sProdImgNo_6 = i['sProdImgNo_6']
        sProdName = i['sProdName']

        # 将内容进行转码
        img_url = urllib.parse.unquote(sProdImgNo_6)
        # img_url.replace('')  # 替换
        img_url = re.sub('200', '0', img_url)

        name = urllib.parse.unquote(sProdName)
        name = re.sub('/0','',name)
        # print(name)

        end = img_url.split('.')[-1]
        end = re.sub('/0','',end)
        print(img_url, name,end)
        # 写入二进制数据,保存图片
        img_url = requests.get(url=img_url,headers=headers).content
        print(f'王者荣耀/{page}-{name}.{end}')
        with open(f'{word}/{page}-{name}.{end}','wb') as f:
            f.write(img_url)
        index += 1

if __name__ == '__main__':
    word = '王者荣耀'
    if not os.path.exists(word):
        os.mkdir(word)
for page in range(33):
    wangzhe(page)
